﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Interfejsi;
using MySql.Data.MySqlClient;
using sistem;

namespace DAL
{
    partial class DAL
    {
        public class AdministratorDAO : IDaoCrud<Administrator>
        {
            #region IDaoCrud<Administrator> Members
            protected MySqlCommand c;
            public long create(Administrator entity)
            {
                try
                {
                    c = new MySqlCommand("insert into administrator (korisnik_id) values (@ID);", con);
                    c.Parameters.AddWithValue("@ID", entity.ID);
                    c.ExecuteNonQuery();
                    return c.LastInsertedId;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Administrator> getAll()
            {
                try
                {
                    c = new MySqlCommand("select * from korisnik, administrator;", con);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Administrator> administratori = new List<Administrator>();
                    while (r.Read())
                    {
                        Administrator administrator = new Administrator();
                        administrator.ID = r.GetInt32("id");
                        administrator.Ime = r.GetString("ime");
                        administrator.Prezime = r.GetString("prezime");
                        administrator.JMBG = r.GetString("jmbg");
                        administrator.DatumRodjenja = r.GetDateTime("datum_rodjenja");
                        administrator.Nacionalnost = r.GetString("nacionalnost");
                        administrator.Adresa = r.GetString("adresa");
                        administrator.Telefon = r.GetString("telefon");
                        administrator.Email = r.GetString("email");
                        administrator.Username = r.GetString("username");
                        administrator.Password = r.GetString("password");
                        administrator.Tip = r.GetString("tip");
                        administrator.Slika = r.GetString("slika");
                        administratori.Add(administrator);
                    }
                    r.Close();
                    c.Dispose();
                    return administratori;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Administrator read(Administrator entity)
            {
                try
                {
                    c = new MySqlCommand("select * from korisnik, administrator where id = @ID and korisnik_id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", entity.ID);
                    MySqlDataReader r = c.ExecuteReader();
                    Administrator administrator = new Administrator();
                    if (r.Read())
                    {
                        administrator.ID = r.GetInt32("id");
                        administrator.Ime = r.GetString("ime");
                        administrator.Prezime = r.GetString("prezime");
                        administrator.JMBG = r.GetString("jmbg");
                        administrator.DatumRodjenja = r.GetDateTime("datum_rodjenja");
                        administrator.Nacionalnost = r.GetString("nacionalnost");
                        administrator.Adresa = r.GetString("adresa");
                        administrator.Telefon = r.GetString("telefon");
                        administrator.Email = r.GetString("email");
                        administrator.Username = r.GetString("username");
                        administrator.Password = r.GetString("password");
                        administrator.Tip = r.GetString("tip");
                        administrator.Slika = r.GetString("slika");
                    }
                    r.Close();
                    c.Dispose();
                    return administrator;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Administrator update(Administrator entity)
            {
                return entity;
            }

            public void delete(Administrator entity)
            {
                try
                {
                    c = new MySqlCommand("delete from administrator where id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", entity.ID);
                    c.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Administrator getById(int id)
            {
                try
                {
                    c = new MySqlCommand("select * from korisnik, administrator where id = @ID and korisnik_id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", id);
                    MySqlDataReader r = c.ExecuteReader();
                    Administrator administrator = new Administrator();
                    if (r.Read())
                    {
                        administrator.ID = r.GetInt32("id");
                        administrator.Ime = r.GetString("ime");
                        administrator.Prezime = r.GetString("prezime");
                        administrator.JMBG = r.GetString("jmbg");
                        administrator.DatumRodjenja = r.GetDateTime("datum_rodjenja");
                        administrator.Nacionalnost = r.GetString("nacionalnost");
                        administrator.Adresa = r.GetString("adresa");
                        administrator.Telefon = r.GetString("telefon");
                        administrator.Email = r.GetString("email");
                        administrator.Username = r.GetString("username");
                        administrator.Password = r.GetString("password");
                        administrator.Tip = r.GetString("tip");
                        administrator.Slika = r.GetString("slika");
                    }
                    r.Close();
                    c.Dispose();
                    return administrator;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Administrator> getByExample(string name, string value)
            {
                try
                {
                    c = new MySqlCommand("select * from korisnik, administrator where @Kolona = @Vrijednost and id = korisnik_id;", con);
                    c.Parameters.AddWithValue("@Kolona", name);
                    c.Parameters.AddWithValue("@Vrijednost", value);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Administrator> administratori = new List<Administrator>();
                    while (r.Read())
                    {
                        Administrator administrator = new Administrator();
                        administrator.ID = r.GetInt32("id");
                        administrator.Ime = r.GetString("ime");
                        administrator.Prezime = r.GetString("prezime");
                        administrator.JMBG = r.GetString("jmbg");
                        administrator.DatumRodjenja = r.GetDateTime("datum_rodjenja");
                        administrator.Nacionalnost = r.GetString("nacionalnost");
                        administrator.Adresa = r.GetString("adresa");
                        administrator.Telefon = r.GetString("telefon");
                        administrator.Email = r.GetString("email");
                        administrator.Username = r.GetString("username");
                        administrator.Password = r.GetString("password");
                        administrator.Tip = r.GetString("tip");
                        administrator.Slika = r.GetString("slika");
                        administratori.Add(administrator);
                    }
                    r.Close();
                    c.Dispose();
                    return administratori;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            #endregion
        }
    }
}
